/***************************************************************************************************
 *
 * 
 *
 ***************************************************************************************************/
function users_load_all()
{
	new Ajax.Request('index.php?c=ajax_users&a=users_get_all', {
  	method:'get',
  	onSuccess: function(transport){
     	var json = transport.responseText.evalJSON();
			users_list_refresh(json);
   	}
	});
}

/*
 *
 *
 */
function users_init()
{
	users_load_all();
}

/*
 *
 *
 */
function users_form_init()
{
	$('user_save').onclick = function(){
		form = $('user_form');
		form.submit()
		}

	$('user_cancel').onclick = function(){
		document.location ='index.php?c=ajax_users'; 
	}

	$('user_delete').onclick = function(){
		if(confirm('Eliminar el usuario'))
		{
			new Ajax.Request('index.php?c=ajax_users&a=user_delete&id=' + ITEM_ID, {
  			method:'get',
  			onSuccess: function(transport){
					document.location ='index.php?c=ajax_users'; 
				}
				});
		}
	}
}

/*
 *
 *
 */
function user_init()
{
	users_form_init();
}


/*
 *
 *
 */
function users_list_refresh(items)
{
	// Sortable.destroy('item_list');

	var ul = $('users_list');
	var lis = $A(ul.childNodes);
	lis.each(function(item){
			ul.removeChild(item);
			item = null;
		}
	);

	items.each(function(item){

    var im_delete = document.createElement('img');
    im_delete.src = '../public/icons/16x16/apps/delete.png';
		im_delete.onclick = function(){
			if(confirm('Eliminar el usuario' + item.name + ', ' + item.lastname))
			{
				new Ajax.Request('index.php?c=ajax_users&a=user_delete&id=' + li.name, {
  				method:'get',
  				onSuccess: function(transport){
     			var json = transport.responseText.evalJSON();
					users_load_all();
					}
				});
			}
		}

    var im_edit = document.createElement('img');
    im_edit.src = '../public/icons/16x16/actions/edit.png';
		im_edit.onclick = function(){
			document.location = 'index.php?c=ajax_users&a=user&id=' + li.name;
		}

    var tx = document.createElement('text');
		tx.innerHTML = item.name + ', ' + item.lastname;

		var li = document.createElement('li');
		li.appendChild(im_delete);
		li.appendChild(im_edit);
		li.appendChild(tx);
		li.name = item.id;
		ul.appendChild(li);
	})
}


